/**
 * <p>Copyright (c) 2017 深圳市鹏途交通科技有限公司 </p>
 * <p>				   All right reserved. 		     </p>
 * 
 * <p>项目名称 ： 	深圳市国省检日常化监管系统         </p>
 * <p>创建者   :	win7 
 * 
 * <p>描   述  :   PreventiveConServiceImpl.java for com.pengtu.gsj.service.impl.check.jsbz.preventMaintain    </p>
 * 
 * <p>最后修改 : $: 2017年8月10日-上午10:21:09 v 1.0.0	 win7   $ </p>
 * 
*/

package com.pengtu.gsj.service.impl.check.jsbz.preventMaintain;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONObject;
import com.pengtu.gsj.dao.base.QueryFilter;
import com.pengtu.gsj.dao.base.QueryFilter.MatchType;
import com.pengtu.gsj.dao.check.jsbz.preventMaintain.PreventiveConDao;
import com.pengtu.gsj.entity.app.Attachment;
import com.pengtu.gsj.entity.check.CheckTable;
import com.pengtu.gsj.entity.jsbz.PrevenManagement;
import com.pengtu.gsj.service.check.CheckTableService;
import com.pengtu.gsj.service.check.jsbz.preventMaintain.PreventiveConService;
import com.pengtu.gsj.service.system.SystemManageService;
import com.pengtu.gsj.utils.MathsUtils;

/**
 * 
 * PreventiveConServiceImpl
 * 
 * 2017年8月10日 上午10:21:09
 * 
 * @version 1.0.0
 * 
 */
@Service
@Transactional
public class PreventiveConServiceImpl implements PreventiveConService {
	
	@Autowired
	private SystemManageService systemManageService;
	
	@Autowired
	public PreventiveConDao preventiveConDao;
	
	@Autowired
	private CheckTableService checkTableService;
	
	public List<PrevenManagement> getgetPreventiveConList(QueryFilter queryFilter){
		return preventiveConDao.find(queryFilter,"testYear", true);
	}
	/**
	 * 查询，根据查询条件获取结果
	 */
	@Override
	public Map<String, Object> getPreventiveCon(String startTime, String endTime, CheckTable checkTable, String tag)throws Exception{
		Map<String, Object> map = new HashMap<String, Object>();
		List<PrevenManagement> prevenManagementList = new ArrayList<PrevenManagement>();
		List<Attachment> list = new ArrayList<Attachment>();
		
		JSONObject jsonObject = JSONObject.parseObject(checkTable.getRetrieval());
		double yearFull = Double.parseDouble((String)jsonObject.get("yearFull"));
		double yearPart = Double.parseDouble((String)jsonObject.get("yearPart"));
		double poitMark = Double.parseDouble((String)jsonObject.get("poitMark"));
		double reserveMark =  Double.parseDouble((String)jsonObject.get("reserveMark"));
		double firstStepScore =  Double.parseDouble((String)jsonObject.get("firstStepScore"));
		
		//取到所有文件
		list = systemManageService.getAllCheckAttachments(startTime, endTime, tag);
		//取得预防性养护意见文件
		List<Attachment> yfyhList = systemManageService.getCheckAttachmentByTag(startTime, endTime, "yhgl_jszj");
		//查询数据
		QueryFilter queryFilter = new QueryFilter();
		queryFilter.addFilter("testYear", startTime, MatchType.GE);
		queryFilter.addFilter("testYear", endTime, MatchType.LE);
		//检查记录
		double total = 0.0;				//得分
		double percentage = 0.0;		//完善度
		double tableFull = MathsUtils.sumDouble(yearFull, yearPart);
		tableFull = MathsUtils.sumDouble(tableFull, reserveMark);
		tableFull = MathsUtils.formatDouble(tableFull, 2);         //满分
		
		prevenManagementList = this.getgetPreventiveConList(queryFilter);
		double sumMail = 0;				//沥青总里程
		double defenceMail = 0;			//养护总里程
		double mailPate = 0;			//五年平比例
		double temp = 0;				//年度总分
		double attMark = 0;             //文件得分
		double yearMark =0;				//年均得分
		for (PrevenManagement prevenManagement : prevenManagementList) {
			if (prevenManagement.getSumAsphalt() != null) {
				sumMail = MathsUtils.sumDouble(sumMail, prevenManagement.getSumAsphalt());
			} 
			
			if (prevenManagement.getDefendAsphalt() != null) {
				defenceMail = MathsUtils.sumDouble(defenceMail, prevenManagement.getDefendAsphalt());
			}
		}
		mailPate = MathsUtils.divDouble(defenceMail, sumMail, 2);
		if (prevenManagementList.size() >= 5) {
			total = MathsUtils.sumDouble(total, yearFull);
			temp = MathsUtils.sumDouble(temp, yearFull);
			yearMark = MathsUtils.sumDouble(yearMark, yearFull);
			if (mailPate >= 0.05) {
				total = MathsUtils.sumDouble(total, yearPart);
				temp = MathsUtils.sumDouble(temp, yearPart);
			} else if (MathsUtils.subDouble(0.05, mailPate)*poitMark*100 < yearPart) {
				//total += yearPart- MathsUtils.subDouble(0.05, mailPate)*poitMark*100;
				total = MathsUtils.sumDouble(total, MathsUtils.subDouble(yearPart, MathsUtils.subDouble(0.05, mailPate)*poitMark*100));
				//temp += yearPart- MathsUtils.subDouble(0.05, mailPate)*poitMark*100;
				temp = MathsUtils.sumDouble(temp, MathsUtils.subDouble(yearPart, MathsUtils.subDouble(0.05, mailPate)*poitMark*100));
			} 
		}
		
		if (yfyhList.size() >= 3) {
			total = MathsUtils.sumDouble(total, reserveMark);
			attMark = MathsUtils.sumDouble(attMark, reserveMark);
		} else if (yfyhList.size() < 3 && yfyhList.size() > 0) {
			total = MathsUtils.sumDouble(total, firstStepScore);
			attMark = MathsUtils.sumDouble(attMark, firstStepScore);
		}
		map.put("sum", total);
		//判断完善度
		percentage = MathsUtils.divDouble(total, tableFull, 2);
		percentage = MathsUtils.mulDouble(percentage, 100);
		map.put("percent", percentage);
		
		map.put("temp", temp);
		map.put("att", list);
		map.put("sumMail", sumMail);
		map.put("defenceMail", defenceMail);
		map.put("mailPate", mailPate*100);
		map.put("attMark", attMark);
		map.put("yearMark", yearMark);
		map.put("yfyhList", yfyhList);
		map.put("checkTable", checkTable);
		map.put("changeInves", prevenManagementList);
		checkTableService.updateCheckTable(checkTable, percentage, total, tableFull, true);
		map.put("score", checkTable.getTestFull());
		return map;
	}
}
